package com.x.springboot.controller;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.x.springboot.common.CommonResult;
import com.x.springboot.model.User;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.*;
import com.github.pagehelper.PageInfo;
import com.x.springboot.service.UserService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author x.li
 * @since 2021-06-04
 */
@RestController
@RequestMapping("/user")
@Api(tags = "用户管理API")
public class UserController {
    private static final Logger LOGGER = LoggerFactory.getLogger(UserController.class);
    private static final String APPID = "wx3962309fe5e32006";
    private static final String SECRET  = "99973e320896b48bf477b41cf7f6bce7";
    @Autowired
    private UserService userService;

    @ApiOperation(value = "新增", httpMethod = "POST")
    @PostMapping(value = "/create")
    public CommonResult create(@RequestBody User user) {
        userService.create(user);
        return CommonResult.success();
    }

    @ApiOperation(value = "根据ID删除", httpMethod = "POST")
    @PostMapping(value = "/delete")
    public CommonResult deleteBatch(@RequestBody Integer id) {
        userService.delete(id);
        return CommonResult.success();
    }

    @ApiOperation(value = "根据ID集合批量删除", httpMethod = "POST")
    @PostMapping(value = "/deleteBatch")
    public CommonResult deleteBatch(@RequestBody List<Integer> ids) {
        userService.deleteBatch(ids);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改", httpMethod = "POST")
    @PostMapping(value = "/update")
    public CommonResult update(@RequestBody User user) {
        userService.update(user);
        return CommonResult.success();
    }

    @ApiOperation(value = "根据主键ID查询", httpMethod = "GET")
    @GetMapping(value = "/get")
    public CommonResult<User> get(@ApiParam(value="id") @RequestParam(value = "id",required = true) Long id) {
        return CommonResult.success(userService.get(id));
    }

    @ApiImplicitParams({ @ApiImplicitParam(name = "pageNum", value = "当前页码", required = true, dataType = "int"),
    @ApiImplicitParam(name = "pageSize", value = "每页显示的条数", required = true, dataType = "int")})
    @ApiOperation(value = "分页查询", httpMethod = "GET")
    @GetMapping(value = "/query")
    public CommonResult<PageInfo<User>> query(User user,int pageNum,int pageSize) {
        PageInfo<User> pageInfo = userService.query(user,pageNum,pageSize);
        return CommonResult.success(pageInfo);
    }
}